package com.fjec.gateway.controller;

import com.fjec.common.protocol.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Slf4j
@RestController
public class ErrorHandleController implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("/error")
    public Object error(HttpServletRequest request){
        Object objStatus = request.getAttribute("javax.servlet.error.status_code");
        Object objMsg = request.getAttribute("javax.servlet.error.message");
        log.error(String.valueOf(objStatus), objMsg);
        return JsonResult.failure()
                .code(String.valueOf(objStatus))
                .message(String.valueOf(objMsg))
                .build();
    }

}
